using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace GameFrameworkLibraly.Helpers
{
    public class IsolatedStorageHelper<T> where T : class, new()
    {
        public static string FileName = "";
        public static string FileDirectory = "";

        private static readonly object LockFlag = new object();
        private static T _instance;

        #region Function
        
        /// <summary>
        /// Loads instance from file or creates a new one if file doesn't exist
        /// </summary>
        [XmlIgnore]
        public static T Instance
        {
            get { return Init(); }
        }

        private static T Init()
        {
            lock (LockFlag)
            {

                if (_instance == null)
                {
                    try
                    {
                        using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            using (IsolatedStorageFileStream stream = storage.OpenFile(FullPathToFile, FileMode.Open))
                            {
                                DataContractSerializer xml = new DataContractSerializer(typeof(T));
                                _instance = xml.ReadObject(stream) as T;
                            }
                        }

                    }
                    catch(Exception ex)
                    {

                        _instance = new T();
                    }
                }
            }
            return _instance;
        }

        /// <summary>
        /// Save instance to file
        /// </summary>
        public static void Save()
        {
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if(!storage.DirectoryExists(FileDirectory))
                    storage.CreateDirectory(FileDirectory);
                using (IsolatedStorageFileStream stream = storage.CreateFile(FullPathToFile))
                {
                    DataContractSerializer xml = new DataContractSerializer(typeof(T));
                    xml.WriteObject(stream, Instance);
                }
            }
        }

        /// <summary>
        /// Replace current instance with a new one
        /// </summary>
        /// <param name="newInstance">New instance</param>
        public static void NewInstance(T newInstance)
        {
            _instance = newInstance;
        }

        /// <summary>
        /// Reload instance from file
        /// </summary>
        public static void Reload()
        {
            _instance = null;
            Init();
        }

        #endregion

        /// <summary>
        /// returns full path to file
        /// </summary>
        public static string FullPathToFile
        {
            get { return Path.Combine(FileDirectory, FileName); }
        }

    }
}
